package com.y2.authprj.mvc.controller.auth;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.gdglc.spring.biz.auth.FunctionauthorityBiz;
import com.gdglc.spring.biz.auth.IRoleAuthorityBiz;
import com.gdglc.spring.biz.auth.ISysroleBiz;
import com.gdglc.spring.biz.auth.PageBiz;
import com.y2.authprj.bean.auth.model.FunctionauthorityInfo;
import com.y2.authprj.bean.auth.model.PagedResult;
import com.y2.authprj.bean.auth.model.RoleauthorityInfo;
import com.y2.authprj.bean.auth.model.SysroleInfo;
import com.y2.authprj.bean.auth.vo.PageAuthority;
import com.y2.authprj.bean.auth.vo.ShowRoleByMulitTableVo;
import com.y2.authprj.bean.auth.vo.showSysroleAndRoleAuthorityVo;

@Controller
@RequestMapping("/RoleAuthority")
public class RoleAuthorityController {
	@Resource(name="IRoleAuthorityBiz")
	private IRoleAuthorityBiz biz;
	@Resource(name="PageBiz")
	private PageBiz biz2;
	@Resource(name="functionBiz")
	private FunctionauthorityBiz funBiz;
	@Resource(name="ISysroleBiz")
	private ISysroleBiz sysBiz;
	
	@RequestMapping("/show")
	public String show(Model model){
		List<ShowRoleByMulitTableVo> roleauthorityInfos=biz.findRoleAuthorityByMultiTable();
		model.addAttribute("roleauthorityInfos", roleauthorityInfos);
		return "auth/RoleAuthority";
	}
	
	@RequestMapping("/page")
	public String page(PageAuthority page,Model model,Integer FunPageNum){
		Integer pageSize=5;
		
		PagedResult<ShowRoleByMulitTableVo> pageFun=biz2.queryAuthorityByPage(page, FunPageNum, pageSize);
		model.addAttribute("roleauthorityInfos", pageFun.getDataList());
		model.addAttribute("funPage", pageFun);
		model.addAttribute("page", page);
		
		return "auth/RoleAuthority";
	}

	@RequestMapping("/del")
	public String deleteRoleAuthority(@RequestParam(value="dat",required=false) String[] datas){
		if(datas!=null){
			for(int i=0;i<datas.length;i++){
				int id=Integer.parseInt(datas[i].toString()) ;
				int count=biz.deleteRoleAuthority(id);
			}
		}
		
		return "redirect:/RoleAuthority/page";
	}
	
	@RequestMapping("/update")
	public String updateRoleAuthority(RoleauthorityInfo roleauthorityInfo){
		int i=biz.updateRoleAuthority(roleauthorityInfo);
		return "redirect:/RoleAuthority/page";
	}
	
	
	
	
	@RequestMapping("/showAdd")
	public String showAddAuthority(@RequestParam(value="SysRoleName",required=false) String sysName,@RequestParam(value="FunName",required=false) String funName,Model model
			,Integer FunPageNum,Integer SysPageNum){
		Integer pageSize=5;
		FunctionauthorityInfo functionauthorityInfo=new FunctionauthorityInfo();
		functionauthorityInfo.setName(funName);
		PagedResult<FunctionauthorityInfo> pageFun=biz2.queryFunctionauthorityByPage(funName, FunPageNum, pageSize);
		model.addAttribute("functionauthorityInfo", functionauthorityInfo);
		model.addAttribute("funs", pageFun.getDataList());
		model.addAttribute("funPage", pageFun);
		
		SysroleInfo sysroleInfo=new SysroleInfo();
		sysroleInfo.setName(sysName);
		PagedResult<SysroleInfo> pageSys=biz2.queryFunctionauthorityByNameByPage(sysName, SysPageNum, pageSize);
		model.addAttribute("sysroleInfo", sysroleInfo);
		model.addAttribute("SysInfo", pageSys.getDataList());
		model.addAttribute("sysPage", pageSys);
		return "auth/AddAndUpdateRoleAuthority";
	}
	
	
	@RequestMapping("/add")
	@ResponseBody
	public String addRoleAuthority(@RequestParam(value="dat[]",required=false) long[] datas,Integer sysRoleId,Model model){
		String object="true";
		if(datas!=null){
			Integer funId = 0;
			for (int i = 0; i < datas.length; i++) 
				funId = Integer.parseInt(String.valueOf(datas[i]));
				RoleauthorityInfo roleauthorityInfo=biz.findRoleAuthorityByRoleIdAndFunId(sysRoleId, funId);
				if(roleauthorityInfo!=null){
					object="false";
					return object;
				}
			}
			for (int i = 0; i < datas.length; i++) {
				RoleauthorityInfo r=new RoleauthorityInfo();
				r.setFunctionauthorityId(Integer.parseInt(String.valueOf(datas[i])));
				r.setRoleId(sysRoleId);
				r.setDeleteFlag(1);
				r.setCreatetTime(new Date());
				biz.addRoleAuthority(r);
			}
		
		return object;
	}
}

